package com.sun.lib;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.util.Log;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.orhanobut.logger.Logger;
import com.sun.lib.util.ToastUtil;

import java.util.Iterator;
import java.util.List;

/*
 * Created by Administrator on 2019/7/29.
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public class LibCommonManager {
    private Context context;

    private static class LibCommonHolder {
        private static LibCommonManager instance = new LibCommonManager();
    }

    public static LibCommonManager getInstance() {
        return LibCommonHolder.instance;
    }

    private LibCommonManager() {
    }

    public void init(@NonNull Application context) {
        this.context = context.getApplicationContext();
        initArouter(context);
        Logger.init();
    }

    private void initArouter(@NonNull Application context) {
        ARouter.openDebug();
        ARouter.openLog();
        ARouter.init(context);
    }

    public static Context getContext() {
        checkContext();
        return getInstance().context;
    }

    private static void checkContext() {
        if (getInstance().context == null)
            throw new RuntimeException("please first init!");
    }

    public static void showToast(String message) {
        ToastUtil.show(getContext(), message);
    }

    public static boolean isMainProcess(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String mainProcessName = context.getPackageName();
        int myPid = Process.myPid();
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        if (processInfos == null) {
            Log.i("TAG", "isMainProcess get getRunningAppProcesses null");
            List<ActivityManager.RunningServiceInfo> processList = am.getRunningServices(2147483647);
            if (processList == null) {
                Log.i("TAG", "isMainProcess get getRunningServices null");
                return false;
            } else {
                Iterator var9 = processList.iterator();

                ActivityManager.RunningServiceInfo rsi;
                do {
                    if (!var9.hasNext()) {
                        return false;
                    }

                    rsi = (ActivityManager.RunningServiceInfo) var9.next();
                } while (rsi.pid != myPid || !mainProcessName.equals(rsi.service.getPackageName()));

                return true;
            }
        } else {
            Iterator var5 = processInfos.iterator();

            ActivityManager.RunningAppProcessInfo info;
            do {
                if (!var5.hasNext()) {
                    return false;
                }

                info = (ActivityManager.RunningAppProcessInfo) var5.next();
            } while (info.pid != myPid || !mainProcessName.equals(info.processName));

            return true;
        }
    }
}
